using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using PickGold.Charting;

namespace PickGold.Charting.Formulas
{
	internal class SeriesFieldList : List<SeriesFieldInfo>
	{
		private static void AddSeriesFieldInfo(SeriesFieldList result, string seriesName, IList<DataField> unmappedFormulaFields)
		{
			List<DataField> list = new List<DataField>(FormulaHelper.GetDataFields(FormulaHelper.GetDefaultChartType(unmappedFormulaFields[0])));
			int num = 0;
			while ((num < unmappedFormulaFields.Count) && (list.Count > 0))
			{
				DataField item = unmappedFormulaFields[num];
				DataField? nullable = null;
				if (list.Contains(item))
				{
					nullable = new DataField?(item);
				}
				if (nullable.HasValue)
				{
					result.Add(new SeriesFieldInfo(seriesName, nullable.Value));
					list.Remove(item);
					unmappedFormulaFields.Remove(item);
				}
				else
				{
					num++;
				}
			}
		}

		private static void AddSeriesFieldInfo(SeriesFieldList result, Series series, IList<DataField> unmappedFormulaFields)
		{
			List<DataField> list = new List<DataField>(FormulaHelper.GetDataFields(series.ChartType));
			int num = 0;
			while ((num < unmappedFormulaFields.Count) && (list.Count > 0))
			{
				DataField item = unmappedFormulaFields[num];
				DataField? nullable = null;
				if (list.Contains(item))
				{
					nullable = new DataField?(item);
				}
				if (!nullable.HasValue)
				{
					nullable = FormulaHelper.MapFormulaDataField(series.ChartType, item);
				}
				if (nullable.HasValue)
				{
					result.Add(new SeriesFieldInfo(series, nullable.Value));
					list.Remove(item);
					unmappedFormulaFields.Remove(item);
				}
				else
				{
					num++;
				}
			}
		}

		private static void AddSeriesFieldInfo(SeriesFieldList result, string seriesName, IList<DataField> unmappedFormulaFields, string seriesFieldId)
		{
			IList<DataField> dataFields = FormulaHelper.GetDataFields(FormulaHelper.GetDefaultChartType(unmappedFormulaFields[0]));
			DataField? nullable = null;
			seriesFieldId = seriesFieldId.ToUpperInvariant().Trim();
			if (seriesFieldId == "Y")
			{
				nullable = new DataField?(dataFields[0]);
			}
			else if (seriesFieldId.StartsWith("Y", StringComparison.Ordinal))
			{
				int num = 0;
				if (int.TryParse(seriesFieldId.Substring(1), out num))
				{
					if (num >= dataFields.Count)
					{
						throw new ArgumentException(SR.ExceptionFormulaYIndexInvalid, seriesFieldId);
					}
					nullable = new DataField?(dataFields[num - 1]);
				}
			}
			else
			{
				try
				{
					nullable = new DataField?((DataField)Enum.Parse(typeof(DataField), seriesFieldId, true));
				}
				catch (ArgumentException)
				{
				}
			}
			if (!nullable.HasValue)
			{
				throw new ArgumentException(SR.ExceptionDataPointValueNameInvalid, seriesFieldId);
			}
			result.Add(new SeriesFieldInfo(seriesName, nullable.Value));
			unmappedFormulaFields.Remove(nullable.Value);
		}

		private static void AddSeriesFieldInfo(SeriesFieldList result, Series series, IList<DataField> unmappedFormulaFields, string seriesFieldId)
		{
			IList<DataField> dataFields = FormulaHelper.GetDataFields(series.ChartType);
			DataField? nullable = null;
			seriesFieldId = seriesFieldId.ToUpperInvariant().Trim();
			if (seriesFieldId == "Y")
			{
				nullable = new DataField?(dataFields[0]);
			}
			else if (seriesFieldId.StartsWith("Y", StringComparison.Ordinal))
			{
				int num = 0;
				if (int.TryParse(seriesFieldId.Substring(1), out num))
				{
					if ((num - 1) >= dataFields.Count)
					{
						throw new ArgumentException(SR.ExceptionFormulaYIndexInvalid, seriesFieldId);
					}
					nullable = new DataField?(dataFields[num - 1]);
				}
			}
			else
			{
				nullable = new DataField?((DataField)Enum.Parse(typeof(DataField), seriesFieldId, true));
			}
			if (!nullable.HasValue)
			{
				throw new ArgumentException(SR.ExceptionDataPointValueNameInvalid, seriesFieldId);
			}
			result.Add(new SeriesFieldInfo(series, nullable.Value));
			if (unmappedFormulaFields.Contains(nullable.Value))
			{
				unmappedFormulaFields.Remove(nullable.Value);
			}
			else
			{
				unmappedFormulaFields.RemoveAt(0);
			}
		}

		public static SeriesFieldList FromString(Chart chart, string seriesFields, IList<DataField> formulaFields)
		{
			SeriesFieldList result = new SeriesFieldList();
			if (!string.IsNullOrEmpty(seriesFields))
			{
				List<DataField> unmappedFormulaFields = new List<DataField>(formulaFields);
				foreach (string str in seriesFields.Split(new char[] { ',' }))
				{
					if (unmappedFormulaFields.Count == 0)
					{
						return result;
					}
					string[] strArray = str.Split(new char[] { ':' });
					if (strArray.Length > 2)
					{
						throw new ArgumentException(SR.ExceptionFormulaDataFormatInvalid(str));
					}
					string name = strArray[0].Trim();
					Series series = chart.Series.FindByName(name);
					if (series != null)
					{
						switch (strArray.Length)
						{
							case 1:
								AddSeriesFieldInfo(result, series, unmappedFormulaFields);
								goto Label_00EE;

							case 2:
								AddSeriesFieldInfo(result, series, unmappedFormulaFields, strArray[1]);
								goto Label_00EE;
						}
					}
					else
					{
						switch (strArray.Length)
						{
							case 1:
								AddSeriesFieldInfo(result, name, unmappedFormulaFields);
								goto Label_00EE;

							case 2:
								AddSeriesFieldInfo(result, name, unmappedFormulaFields, strArray[1]);
								goto Label_00EE;
						}
					}
				Label_00EE: ;
				}
			}
			return result;
		}

		public override string ToString()
		{
			StringBuilder builder = new StringBuilder();
			for (int i = 0; i < base.Count; i++)
			{
				SeriesFieldInfo info = base[i];
				if (i > 0)
				{
					builder.Append(',');
				}
				SeriesChartType chartType = (info.Series != null) ? info.Series.ChartType : FormulaHelper.GetDefaultChartType(info.DataField);
				int index = FormulaHelper.GetDataFields(chartType).IndexOf(info.DataField);
				if (index == 0)
				{
					builder.AppendFormat(CultureInfo.InvariantCulture, "{0}:Y", new object[] { info.SeriesName });
				}
				else
				{
					builder.AppendFormat(CultureInfo.InvariantCulture, "{0}:Y{1}", new object[] { info.SeriesName, index + 1 });
				}
			}
			return builder.ToString();
		}
	}
}

